package cn.edu.tjut.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.tjut.bean.TeacherBean;
import cn.edu.tjut.myUtils.MD5Util;
import cn.edu.tjut.myUtils.MyUtils;
import cn.edu.tjut.seriver.TeaService;

@Controller
public class TeaInfoControl {
	// *************教师信息管理**************************************//
	@Resource
	private TeaService teaService;

	// 跳转添加老师信息界面
	@RequestMapping(value = "/insertTeaInfo.do")
	public String insertTeaInfo0() {
		return "teaInfoManager/insertTeaInfo";
	}

	// 添加实验老师信息
	@RequestMapping(value = "/Teadetails.do")
	public String insertTeaInfo1(TeacherBean teaB, HttpSession session, Model model) {
		if (teaB.getTeaIndex() == null) {
			// 添加教师信息
			String md5psw = MD5Util.MD5(teaB.getTeaPsw());
			teaB.setTeaPsw(md5psw);
			int i = teaService.insertTeaInfo(teaB);
			if (i > 0) {
				List<TeacherBean> lstTea = teaService.selectAllteas();
				if (lstTea != null) {
					session.setAttribute("lstTea", lstTea);
					return "teaInfoManager/teaInfoDetails";
				} else {
					session.setAttribute("Prompt", "暂时没有实验老师信息");
					return "teaInfoManager/teaInfoDetails";
				}
			} else {
				session.setAttribute("Prompt", "添加实验老师信息失败！");
				return "teaInfoManager/teaInfoDetails";
			}
		}
		// 修改实验老师信息
		// 修改信息时如果修改密码，则将新密码设置MD5加密，如果不修改则不用设置。MD5密码长度统一为32位
		if (teaB.getTeaPsw().length() < 30) {
			String MD5psw = MD5Util.MD5(teaB.getTeaPsw());
			teaB.setTeaPsw(MD5psw);
		}
		int i = teaService.updateByteaIndex(teaB);
		if (i > 0) {
			List<TeacherBean> lstTea = teaService.selectAllteas();
			if (lstTea != null) {
				session.setAttribute("lstTea", lstTea);
				return "teaInfoManager/teaInfoDetails";
			} else {
				session.setAttribute("Prompt", "暂时没有实验老师信息");
				return "teaInfoManager/teaInfoDetails";
			}
		} else {
			session.setAttribute("Prompt", "修改实验老师信息失败！");
			return "teaInfoManager/teaInfoDetails";
		}
	}

	// 修改实验老师信息
	@RequestMapping(value = "/updateTeaInfo.do")
	public String updateTeaInfo(TeacherBean teaB, HttpServletRequest request, HttpServletResponse response) {
		String teaIndex = request.getParameter("ope");
		teaB = teaService.selectByteaIndex(teaIndex);
		request.setAttribute("teaB", teaB);
		String opt = request.getParameter("opt");
		if (opt.equals("1")) {
			// 实验老师修改自己的信息
			/********************************************/
			return "";
		}
		return "teaInfoManager/insertTeaInfo";
	}

	// 删除实验老师信息
	@RequestMapping(value = "/deleteTeaInfo.do")
	public String deleteTeaInfo(TeacherBean teaB, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		String teaIndex = request.getParameter("ope");
		teaB.setTeaIndex(MyUtils.obj2Integer(teaIndex));
		teaService.deleteByteaIndex(teaB);
		List<TeacherBean> lstTea = teaService.selectAllteas();
		if (lstTea != null) {
			// 删除一个信息后，重新查询所有老师信息并显示
			return selectAllTeaInfo(session);
		} else {
			session.setAttribute("Prompt", "暂时没有实验老师信息");
			return "teaInfoManager/teaInfoDetails";
		}
	}

	// 查询所有实验教师信息
	@RequestMapping(value = "/selectTea.do")
	public String selectAllTeaInfo(HttpSession session) {
		List<TeacherBean> lstTea = teaService.selectAllteas();
		if (lstTea != null) {
			session.setAttribute("lstTea", lstTea);
			return "teaInfoManager/teaInfoDetails";
		} else {
			session.setAttribute("Prompt", "暂时没有实验老师信息");
			return "teaInfoManager/teaInfoDetails";
		}
	}

	// 动态查询实验老师的信息
	@RequestMapping(value = "/selectTeaDynamic.do")
	public String selectTeaDynamic0(TeacherBean teaB) {
		return "teaInfoManager/selectTeaDynamic";
	}

	// 动态查询实验老师信息
	@RequestMapping(value = "/TeaDynamicdetails.do")
	public String selectTeaDynamic(TeacherBean teaB, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		List<TeacherBean> lstTea = teaService.selectTeaInfoDynamic(teaB);
		if (lstTea != null) {
			session.setAttribute("lstTea", lstTea);
			return "teaInfoManager/teaInfoDetails";
		} else {
			session.setAttribute("Prompt", "暂时没有实验老师信息");
			return "teaInfoManager/teaInfoDetails";
		}
	}

}
